package com.example.administrator.myfragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * Created by wq on 2018/10/30.
 */
public class OneFragment extends Fragment {

    private TextView tv;
    private Button but;
    private boolean ishidden = false;

    public FragmentCallBack callBack;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View messageLayout = inflater.inflate(R.layout.fragment, container, false);
        tv = (TextView) messageLayout.findViewById(R.id.message);
        but = (Button) messageLayout.findViewById(R.id.but);

        tv.setText("首页");
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), OtherActivity.class);
                startActivity(intent);
//                if(callBack!=null) {
//                    callBack.setValue("好好");
//                }
            }
        });


        String message = getArguments().getString("message");
        Log.d("======OneFragment", message);

        Log.d("======OneFragment", "onCreateView");

        return messageLayout;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        callBack = (FragmentCallBack) context;
        Log.d("======OneFragment", "onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("======OneFragment", "onCreate");

        /*but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),OtherActivity.class);
                startActivity(intent);
            }
        });*/
    }

    /**
     * 在这里实现Fragment数据的缓加载.
     *
     * @param isVisibleToUser
     */
    protected boolean isVisible;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            Log.d("=====OneFragment", "getUserVisibleHint");
        } else {
            isVisible = false;
            Log.d("=====OneFragment", "NogetUserVisibleHint");
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
//        super.onHiddenChanged(hidden);
        ishidden = hidden;
        if (!hidden) {
            Log.d("=====OneFragment", "onHiddenChanged");
        }
    }

    /**
     * Fragment是否可见
     *
     * @return
     */
    public boolean isFragmentVisible() {
        if (!ishidden) {
            return true;
        }
        return false;
    }


    @Override
    public void onStart() {
        super.onStart();
        Log.d("======OneFragment", "onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("======OneFragment", "onResume");
        if (isFragmentVisible()) {  //
            Log.d("======OneFragment", "onResume=====================only");
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("======OneFragment", "onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d("======OneFragment", "onStop");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("======onDestroy", "onDestroy");
    }

    //https://www.jianshu.com/p/85e0e53db5f3
    public interface FragmentCallBack {

        void setValue(String str);
    }
}
